package com.tuangou.tuangouheader.header.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuangou.tuangouheader.header.util.MyFileload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import com.tuangou.tuangouheader.header.entity.TuanzhangEntity;
import com.tuangou.tuangouheader.header.service.TuanzhangService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author ycy
 * @email ycy@tuangou.com
 * @date 2021-06-17 09:46:38
 */
@RestController
@RequestMapping("header/tuanzhang")
public class TuanzhangController {
    @Autowired
    private TuanzhangService tuanzhangService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("header:tuanzhang:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tuanzhangService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{tzId}")
    //@RequiresPermissions("header:tuanzhang:info")
    public R info(@PathVariable("tzId") Integer tzId){
		TuanzhangEntity tuanzhang = tuanzhangService.getById(tzId);

        return R.ok().put("tuanzhang", tuanzhang);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("header:tuanzhang:save")
    public R save(@RequestBody TuanzhangEntity tuanzhang){
		tuanzhangService.save(tuanzhang);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("header:tuanzhang:update")
    public R update(@RequestBody TuanzhangEntity tuanzhang){
		tuanzhangService.updateById(tuanzhang);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("header:tuanzhang:delete")
    public R delete(@RequestBody Integer[] tzIds){
		tuanzhangService.removeByIds(Arrays.asList(tzIds));

        return R.ok();
    }


    /**
     * 团长审核通过
     */
    @RequestMapping("/shenhe/{tzId}")
    //@RequiresPermissions("header:tbtuanzhang:info")
    public R shenhetg(@PathVariable("tzId") Integer tzId){
        String tuanzhang = tuanzhangService.shenhetg(tzId);

        return R.ok().put("tuanzhang", tuanzhang);
    }

    /**
     * 团长审核不通过
     */
    @RequestMapping("/bushenhe/{tzId}")
    //@RequiresPermissions("header:tbtuanzhang:info")
    public R shenhebtg(@PathVariable("tzId") Integer tzId){
        String tuanzhang = tuanzhangService.shenhebtg(tzId);

        return R.ok().put("tuanzhang", tuanzhang);
    }

    @RequestMapping("/findAll")
    public R findAll(){
        List<TuanzhangEntity> tuanzhang = tuanzhangService.findAll();
        return R.ok().put("tuanzhang", tuanzhang);
    }

    @RequestMapping("/mohu")
    @ResponseBody
    public R mohu(@RequestParam(value = "page",defaultValue = "1") int page,
                  @RequestParam(value = "size",defaultValue = "1") int size,
                  Date sjx, Date sjd, int status, String name){
        QueryWrapper<TuanzhangEntity> queryWrapper = new QueryWrapper<>();
        if (status==0||status==1||status==2){
            queryWrapper.like("status",status);
        }
        if (name!=null&&!"".equals(name)){
            queryWrapper.like("tz_name",name);
        }
        PageHelper.startPage(page,size);
        List<Map<String, Object>> maps = tuanzhangService.listMaps(queryWrapper);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);

        return R.ok().put("pageInfo",pageInfo);
    }

    /**
     * 团长登录
     */
    @RequestMapping("/login")
    //@RequiresPermissions("header:tuanzhang:save")
    public R login(@RequestBody TuanzhangEntity tuanzhang){
        TuanzhangEntity entity = tuanzhangService.login(tuanzhang);
        if (!ObjectUtils.isEmpty(entity)){
            return R.ok().put("login","登录成功");
        }
        return R.error("登录失败");
    }

    /**
     * 图片上传
     */
    @RequestMapping("/tpsc")
    //@RequiresPermissions("header:tuanzhang:save")
    public R tpsc(@RequestParam("file")MultipartFile file){
        String upload = MyFileload.upload(file);
        return R.ok().put("url",upload);
    }

}
